package com.sora.sora.manager.controller;

import com.sora.sora.common.constant.ResultCode;
import com.sora.sora.common.model.vo.ResultVO;
import com.sora.sora.common.utils.ResponseResult;
import com.sora.sora.common.utils.StringUtils;
import com.sora.sora.manager.service.SysUserService;
import com.sora.sora.security.login.model.param.RegisterParam;
import com.sora.sora.security.login.service.RegisterService;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api(value = "用户管理", tags = "管理端进行用户管理")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    SysUserService sysUserService;

    @Resource
    RegisterService registerService;

    @Resource
    ResponseResult responseResult;

    @Resource
    StringUtils stringUtils;


//    @ApiOperation(value = "根据昵称查找用户，查找结果为数组")
//    @ApiImplicitParam(name = "name", value = "用户昵称", required = true, dataType = "string", paramType = "query")
//    @GetMapping("/query")
//    public ResultVO queryUserByName(@RequestParam("name") String name) {
//        return responseResult.ok(sysUserService.queryUserByName(name));
//    }
//
//
//    @ApiOperation(value = "根据邮箱查找用户")
//    @ApiImplicitParam(name = "email", value = "用户邮箱", required = true, dataType = "string", paramType = "query")
//    @GetMapping("/query")
//    public ResultVO queryUserByEmail(@RequestParam("email") String email) {
//        return responseResult.ok(sysUserService.queryUserByEmail(email));
//    }
//
//
//    @ApiOperation(value = "根据id查找用户")
//    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query")
//    @GetMapping("/query")
//    public ResultVO queryUserById(@RequestParam("uid") int uid) {
//        return responseResult.ok(sysUserService.queryUserById(uid));
//    }


    @ApiOperation(value = "查询用户总数")
    @GetMapping("/count")
    public ResultVO count() {
        return responseResult.ok(sysUserService.count());
    }


    @ApiOperation(value = "按页遍历用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @GetMapping
    public ResultVO getUser(@RequestParam("limit") int limit, @RequestParam("page") int page) {
        return responseResult.ok(sysUserService.queryUser(limit, page));
    }


    @ApiOperation(value = "查找用户，可供查询的参数有: id, name, email")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "参数集合，只解析第一个非空或非零参数", required = true, dataType = "map", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页查询的字段个数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "第几页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询完毕，没有结果"),
            @ApiResponse(code = 201, message = "查询成功，并返回结果"),
            @ApiResponse(code = 1001, message = "查询条件不能为空")
    })
    @GetMapping("/query")
    public ResultVO queryUser(@RequestParam Map<String, Object> params, int page, int limit) {
        if (params.isEmpty()) {
            return responseResult.valid("查询条件不能为空!");
        }

        if (params.containsKey("uid")) {
            int uid = Integer.parseInt((String) params.get("uid"));
            if (uid <= 0) {
                return responseResult.valid("非法入参");
            }
            return responseResult.ok(sysUserService.queryUserById(uid));
        } else if (params.containsKey("name")) {
            String name = (String) params.get("name");
            if (name == null || "".equals(name.trim())) {
                return responseResult.valid("非法入参");
            }
            return responseResult.ok(sysUserService.queryUserByName(name , page, limit));
        } else if (params.containsKey("email")) {
            String email = (String) params.get("email");
            if (email == null || !email.contains("@")) {
                return responseResult.valid("非法入参");
            }
            return responseResult.ok(sysUserService.queryUserByEmail(email));
        }
        return responseResult.ok();
    }

    public ResultVO queryUser(@RequestParam Map<String, Object> params) {
        return queryUser(params, 1, 20);
    }


    @ApiOperation(value = "批量禁用用户")
    @ApiImplicitParam(name = "uids", value = "uid数组", required = true, allowMultiple = true, dataType = "Int", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "禁用成功"),
            @ApiResponse(code = 1002, message = "禁用失败并返回失败用户id")
    })
    @PostMapping("/ban")
    public ResultVO ban(Integer[] uids) {
        if (uids.length <= 0) {
            return responseResult.valid("数组为空");
        }
        List<Integer> list = sysUserService.ban(Arrays.asList(uids));
        return list.isEmpty() ? responseResult.ok() : responseResult.faile(list);
    }


    @ApiOperation(value = "封禁用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "被封禁的用户id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "reason", value = "被封禁的原因", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "封禁时间", required = true, dataType = "string", paramType = "query")
    })
    @DeleteMapping
    public ResultVO ban(@RequestParam("uid") int uid,
                        @RequestParam("reason") String reason,
                        @RequestParam("time") int time) {
        return sysUserService.ban(uid, reason, time) ? responseResult.ok() : responseResult.faile("用户已经在封禁中");
    }

    @ApiOperation(value = "解封用户")
    @ApiImplicitParam(name = "uid", value = "解封用户id", required = true, dataType = "int", paramType = "query")
    @PutMapping
    public ResultVO start(@RequestParam("uid") int uid) {
        return null;
    }


    @ApiOperation(value = "批量启用用户")
    @ApiImplicitParam(name = "uids", value = "uid数组", required = true, allowMultiple = true, dataType = "Int", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "禁用成功"),
            @ApiResponse(code = 1002, message = "禁用失败并返回失败用户id")
    })
    @PutMapping("/ban")
    public ResultVO start(Integer[] uids) {
        if (uids.length <= 0) {
            return responseResult.valid("数组为空");
        }
        List<Integer> list = sysUserService.start(Arrays.asList(uids));
        return list.isEmpty() ? responseResult.ok() : responseResult.faile(list);
    }


    @ApiOperation(value = "重置用户密码，新密码为用户邮箱")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 1002, message = "操作失败")
    })
    @PutMapping("/password")
    public ResultVO resetUserPassword(int uid){
        if (uid <= 0) {
            return new ResultVO(ResultCode.VALIDATE_FAILED, "请输入符合要求的格式");
        }
        boolean flag = sysUserService.resetUserPassword(uid);
        return flag ? responseResult.ok() : responseResult.faile();
    }


    @ApiOperation(value = "重置用户头像")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "int")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改成功"),
            @ApiResponse(code = 1002, message = "操作失败")
    })
    @PutMapping("/avatar")
    public ResultVO resetUserAvatar(int uid) {
        if (uid <= 0) {
            return responseResult.valid("非法uid");
        }
        boolean flag = sysUserService.resetUserAvatar(uid);
        return flag ? responseResult.ok() : responseResult.faile();
    }


    @ApiOperation(value = "管理员创建用户")
    @ApiResponses({
            @ApiResponse(code = 201, message = "创建成功"),
            @ApiResponse(code = 1002, message = "注册失败")
    })
    @PostMapping("/user")
    public ResultVO create(@RequestBody @Validated RegisterParam param) {
        boolean flag = registerService.sysRegister(param);
        return flag ? responseResult.ok() : responseResult.faile();
    }
}
